import _ from 'lodash';
import { message } from 'ant-design-vue';
import emitter, { SELECTLINK_CROSS } from '@/utils/eventBus.js';

export const findFather = (data, lv) => {
  if (data.myLevel === lv) {
    return data;
  } else {
    if (data.fatherNode) {
      return findFather(data.fatherNode, lv);
    }
  }
};

export const isValueFilled = function(value) {
  let flag = false;
  if (value === 0 || value) {
    flag = true;
  }
  return flag;
};

export const setFatherNode = function(data, fatherNode = null, level = 1) {
  if (Array.isArray(data)) {
    data.forEach(item => {
      setFatherNode(item, fatherNode, level);
    });
  } else {
    data.fatherNode = fatherNode;
    data.myLevel = level++;
    if (data.children) {
      setFatherNode(data.children, data, level);
    }
  }
};

// 请求接口时机 && 整理请求接口的数据
export const filterRequestData = function(focusUrlParams, formData, linkItem) {
  const focusUrlParams_cop = _.cloneDeep(focusUrlParams);
  const {
    other,
    type,
    data,
    method
  } = focusUrlParams_cop;
  let flag = true;
  let additionRequest = null;
  let endData = null;

  if ([0].includes(type)) {
    additionRequest = {};
  } else if (type === 1) {
    additionRequest = [];
  }

  if (!additionRequest) { // 没有在接口属性内设置type
    message.warning(linkItem.title + '没有在接口属性内设置type或设置错误(0|1)');
    flag = false;
  }

  if (flag) { // 有type
    if (other && Object.keys(other).length) {
      for (const key in other) {
        const data = formData[key];
        let value = data.myValue;
        // flag = isValueFilled(value);

        const typeEventMap = {
          0: additionRequest => {
            additionRequest[key] = value;
          },

          1: additionRequest => {
            const otherKey = other[key];
            if (otherKey.value) {
              value = data.itemSelected[otherKey.value];
            }

            // additionRequest[key] = value;

            additionRequest.push({
              // col: key,
              col: other[key]['key'],
              value
            });
          }
        };

        if (value) {
          typeEventMap[type](additionRequest);
        } else {
          // flag = false; // 当后台配置了other但是值为空，需要提示用户
          // const msg = other[key].message || `${key}为空（目前没有在后台配置other内dataIndex: ${key},的message属性）`;
          // message.warning(msg);
        }
      }
    }
  }

  if (flag) { // 全部有值
    const requestData = {
      ...data
    };

    if (type === 0) {
      Object.assign(requestData, additionRequest);
    } else if (type === 1) {
      requestData.filterList = additionRequest;
    }

    endData = {
      data: requestData,
      method
    };
  }

  return endData;
  // return flag && endData || flag;
};

//
export const dealRequestData = (focusUrlParams, data) => {
  const { type } = focusUrlParams;
  const paramsArr = Object.keys(data);
  if (type) {
    // 存储过程接口
    focusUrlParams.data.filterList = [];
    paramsArr.forEach(item => {
      focusUrlParams.data.filterList.push({
        col: data[item]['key'],
        value: data[item]['value']
      });
    });
  } else {
    // function接口
    const paramList = [];
    paramsArr.forEach(item => {
      // focusUrlParams.data[other[item]['key']] = data[item];
      paramList.push({
        key: data[item]['key'],
        value: data[item]['value'],
        orderId: data[item]['orderId'],
        type: data[item]['type']
      });
    });
    focusUrlParams.data.paramList = paramList;
  }
  return focusUrlParams;
};

// 表单跨报表
export function linkForReport(linkageField, value, item) {
  const {
    key,
    tableId,
    value: linkValue
  } = linkageField;
  let level = linkageField.level;
  let finalValue = null;

  if (level) {
    level = level * 1;
  }

  if (typeof value !== 'object') {
    finalValue = value;
  } else if (level === 0) {
    finalValue = value[linkValue];
  } else {
    const findItem = findFather(value, level);
    try {
      finalValue = findItem[linkValue];
    } catch (error) {
      message.warning('请检查联动字段中level设置是否有问题');
    }
  }
  if (finalValue) {
    emitter.emit(SELECTLINK_CROSS,
      [{
        tableId,
        key,
        value: finalValue,
        form: item
      }]
    );
  } else {
    message.warning('请检查联动字段中value设置是否有问题, 没有匹配到值为' + linkValue + '的属性');
  }
}
